function busFormatResult(bus) {
	var markup = "<table><tr><th>Placa</th><td>";
	markup += bus.Bus.placa;
	markup += "</td></tr>";
	markup += "<tr><th>Empresa</th><td>";
	markup += bus.Bus.empresa;
	markup += "</td></tr>";
	markup += "<tr><th>Capacidad</th><td>";
	markup += bus.Bus.capacidad;
	markup += "</td></tr>";
	markup += "</table>";
	return markup;
}

function busFormatSelection(bus, container, query) {
	if (bus.length != 0) {
		return bus.Bus.placa;
	}
	return bus;
}

function horarioFormatResult(horario) {
	var markup = "<table><tr><th>Nombre</th><td>";
	markup += horario.Horario.nombre_horario;
	markup += "</td></tr>";
	markup += "<tr><th>Días Aplica</th><td>";
	markup += horario.Horario.dias_aplica;
	markup += "</td></tr>";
	markup += "<tr><th>Hora Salida</th><td>";
	markup += horario.Horario.hora_salida;
	markup += "</td></tr>";
	markup += "<tr><th>Hora Estimada Llegada</th><td>";
	markup += horario.Horario.hora_estimada_llegada;
	markup += "</td></tr>";
	markup += "</table>";
	return markup;
}

function horarioFormatSelection(horario, container, query) {
	if (horario.length != 0) {
		return horario.Horario.nombre_horario;
	}
	return horario;
}

function rutaFormatResult(ruta) {
	var markup = "<table><tr><th>Nombre</th><td>";
	markup += ruta.Ruta.nombre_ruta;
	markup += "</td></tr>";
	markup += "<tr><th>Descripción</th><td>";
	markup += ruta.Ruta.descripcion_ruta;
	markup += "</td></tr>";
	markup += "</table>";
	return markup;
}

function rutaFormatSelection(ruta) {
	if (ruta.length != 0) {
		return ruta.Ruta.nombre_ruta;
	}
	return ruta;
}

$(document).ready(function() {
	$("#RutaIdRuta").select2({
		placeholder: "Escoger...",
		width: "off",
		dropdownAutoWidth: true,
		id: function(object) {
			return object.Ruta.id_ruta;
		},
		initSelection: function(element, callback) {
			var data;
			if (this instanceof Window){
				for (var i in this.app.rutas) {
					if (this.app.rutas[i]["Ruta"]['id_ruta'] == $(element).val()){
						data = this.app.rutas[i];
						break;
					}
				}
			}
			else {
				for (var i in this.data) {
					if (this.data[i]["Ruta"]['id_ruta'] == $(element).val()){
						data = this.data[i];
						break;
					}
				}
			}
	        callback(data);
		},
		query: function(options) {
			if (options.term == "" || options.term === undefined) {
				var result = new Object();
				result.results = this.data;
				options.callback(result);
			}
			else {
				var result = new Object();
				result.results = [];
				for (var i in this.data) {
					for (var propiedad in this.data[i]["Ruta"]) {
						if (this.data[i]["Ruta"][propiedad].toLowerCase().indexOf(options.term.toLowerCase()) != -1) {
							result.results.push(this.data[i]);
							break;
						}
					}
				}
				options.callback(result);
			}
		},
		allowClear: true,
		data: window.app.rutas,
		formatResult: rutaFormatResult,
	    formatSelection: rutaFormatSelection,
		escapeMarkup: function(m) { return m; }
	});
	$("#RutaIdRuta").select2("val", $("#RutaIdRuta").val());

	$("#HorarioIdHorario").select2({
		placeholder: "Escoger...",
		width: "off",
		dropdownAutoWidth: false,
		id: function(object) {
			return object.Horario.id_horario;
		},
		initSelection: function(element, callback) {
			var data;
			if (this instanceof Window){
				for (var i in this.app.horarios) {
					if (this.app.horarios[i]["Horario"]['id_horario'] == $(element).val()){
						data = this.app.horarios[i];
						break;
					}
				}
			}
			else {
				for (var i in this.data) {
					if (this.data[i]["Horario"]['id_horario'] == $(element).val()){
						data = this.data[i];
						break;
					}
				}
			}
	        callback(data);
		},
		query: function(options) {
			if (options.term == "" || options.term === undefined) {
				var result = new Object();
				result.results = this.data;
				options.callback(result);
			}
			else {
				var result = new Object();
				result.results = [];
				for (var i in this.data) {
					for (var propiedad in this.data[i]["Horario"]) {
						if (this.data[i]["Horario"][propiedad].toLowerCase().indexOf(options.term.toLowerCase()) != -1) {
							result.results.push(this.data[i]);
							break;
						}
					}
				}
				options.callback(result);
			}
		},
		allowClear: true,
		data: window.app.horarios,
		formatResult: horarioFormatResult,
	    formatSelection: horarioFormatSelection,
		escapeMarkup: function(m) { return m; }
	});
	
	$("#BusIdBus").select2({
		placeholder: "Escoger...",
		width: "off",
		dropdownAutoWidth: true,
		id: function(object) {
			return object.Bus.id_bus;
		},
		initSelection: function(element, callback) {
			var data;
			if (this instanceof Window){
				for (var i in this.app.horarios) {
					if (this.app.buses[i]["Bus"]['id_bus'] == $(element).val()){
						data = this.app.buses[i];
						break;
					}
				}
			}
			else {
				for (var i in this.data) {
					if (this.data[i]["Bus"]['id_bus'] == $(element).val()){
						data = this.data[i];
						break;
					}
				}
			}
	        callback(data);
		},
		query: function(options) {
			if (options.term == "" || options.term === undefined) {
				var result = new Object();
				result.results = this.data;
				options.callback(result);
			}
			else {
				var result = new Object();
				result.results = [];
				for (var i in this.data) {
					for (var propiedad in this.data[i]["Bus"]) {
						if (this.data[i]["Bus"][propiedad].toLowerCase().indexOf(options.term.toLowerCase()) != -1) {
							result.results.push(this.data[i]);
							break;
						}
					}
				}
				options.callback(result);
			}
		},
		allowClear: true,
		data: window.app.buses,
		formatResult: busFormatResult,
	    formatSelection: busFormatSelection,
		escapeMarkup: function(m) { return m; }
	});
	
});
